<?php

namespace App\Http\Servers\Admin;


use Intervention\Image\Facades\Image;

class IndexServer
{
    private $imageServer;

    public function __construct(ImageServer $imageServer)
    {
        $this->imageServer = $imageServer;
    }

    public function set($request)
    {
        if ($request->hasFile('web_logo')) {
            $tmpWebLogo = $this->imageServer->uploadImage($request->web_logo, 'web_logo', 'web_logo');
            $tmpWebLogo && $update['web_logo'] = $tmpWebLogo;
            $this->imageServer->deleteImage(config('set.web_logo'));
        }
        if ($request->hasFile('web_qrcode')) {
            $tmpWebQrcode = $this->imageServer->uploadImage($request->web_qrcode, 'web_qrcode', 'web_qrcode');
            $tmpWebQrcode && $update['web_qrcode'] = $tmpWebQrcode;
            $this->imageServer->deleteImage(config('set.web_qrcode'));
        }
        if ($request->hasFile('web_ico')) {
            $tmpWebIco = $this->imageServer->uploadImage($request->web_ico, 'web_ico', 'web_ico');
            $tmpWebIco && $update['web_ico'] = $tmpWebIco;
            $this->imageServer->deleteImage(config('set.web_ico'));
        }
        $update['web_name'] = $request->web_name;
        $update['web_url'] = $request->web_url;
        $update['web_title'] = $request->web_title;
        $update['web_keywords'] = $request->web_keywords;
        $update['web_description'] = $request->web_description;
        $update['web_keywords_long'] = $request->web_keywords_long;
        $update['web_email'] = $request->web_email;
        $update['web_phone'] = $request->web_phone;
        $update['web_area'] = rtrim($request->web_area, ',');
        $update['web_default_area'] = $request->web_default_area;
        $update['web_count_url'] = $request->web_count_url;
        $update['web_count_code'] = $request->web_count_code;
        $update['web_kf'] = $request->web_kf;
        $update['wap_theme'] = $request->wap_theme;
        $update['push_token'] = $request->push_token;
        $update['301jump'] = $request['301jump'] ? 1 : 0;
        $update['onepage'] = $request['onepage'] ? 1 : 0;
        $update['wap_jump'] = $request['wap_jump'] ? 1 : 0;

        foreach ($update as $name => $value) {
            \App\Set::where('name', $name)->update(['value' => $value]);
        }

        return true;
    }
}